﻿using JNPF.Basics.Models;
using JNPF.Utils;
using Newtonsoft.Json.Linq;
using System;
using System.Threading.Tasks;

namespace JNPF.Basics.Logic
{
    /// <summary>
    /// 公众号群发消息
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018-09-25 
    /// </summary>
    public class MPMessageBll
    {
        private MPMessageIService service = new MPMessageService();
        private MPMaterialBll mPMaterialBll = new MPMaterialBll();

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="requestParam">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<MPMessageEntity>> GetList(PageInput input)
        {
            try
            {
                return await service.GetList(input);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 预览
        /// </summary>
        /// <param name="openId">公众号用户Id</param>
        /// <param name="entity">实体对象</param>
        public async Task Preview(string openId, MPMessageEntity entity)
        {
            try
            {
                var config = await new SysConfigBll().GetInfo("MPConfig");
                var filePath = "";
                var mediaId = entity.TxtContent;
                var resultJObject = new JObject();
                if (entity.MsgType != 1)//文本
                {
                    UploadFileByMediaId(entity, ref filePath, ref mediaId, ref resultJObject);
                }
                new WeChatMPHelper(config.WX_GZH_APPID, config.WX_GZH_APPSECRET).SendGroupMessagePreview(entity.MsgType, mediaId, openId);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 根据标签进行群发(全部成员)
        /// </summary>
        /// <param name="entity">实体对象</param>
        public async Task SendGroupMessageByTagId(MPMessageEntity entity)
        {
            try
            {
                var config = await new SysConfigBll().GetInfo("MPConfig");
                var filePath = "";
                var mediaId = entity.TxtContent;
                var resultJObject = new JObject();
                bool isToAll = entity.IsToAll == "1" ? true : false;
                if (entity.MsgType != 1)//文本
                {
                    UploadFileByMediaId(entity, ref filePath, ref mediaId, ref resultJObject);
                }
                resultJObject = new WeChatMPHelper(config.WX_GZH_APPID, config.WX_GZH_APPSECRET).SendGroupMessageByTagId(entity.TagId, mediaId, entity.MsgType, isToAll).ToObject();
                var msgid = resultJObject["msgid"].ToString();
                var msgdataid = resultJObject["msgdataid"].ToString();
                entity.MsgId = msgid;
                entity.MsgDataIid = msgdataid;
                entity.Id = CommonHelper.Guid;
                entity.SortCode = CommonHelper.SortCode;
                entity.SendUser = UserProvider.Instance.Get().UserId;
                entity.SendDate = DateTime.Now;
                await service.Create(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }
        /// <summary>
        /// 上传文件获取MediaId
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <param name="filePath">绝对路径</param>
        /// <param name="mediaId">素材Id</param>
        /// <param name="resultJObject">返回Object</param>
        /// <returns></returns>
        private string UploadFileByMediaId(MPMessageEntity entity, ref string filePath, ref string mediaId, ref JObject resultJObject)
        {
            MPMaterialEntity materialEntity = new MPMaterialEntity();
            materialEntity.Title = entity.Title;
            materialEntity.FileJson = entity.FileJson;
            materialEntity.MaterialsType = entity.MsgType;
            switch (entity.MsgType)
            {
                case 4://视频
                    materialEntity.Introduction = entity.TxtContent;
                    mediaId = mPMaterialBll.UploadForeverVideo(materialEntity).Result;
                    break;
                case 5://图文
                    materialEntity.Author = entity.Author;
                    materialEntity.Content = entity.Content;
                    materialEntity.ContentSourceUrl = entity.ContentSourceUrl;
                    materialEntity.Digest = entity.Digest;
                    materialEntity.ShowCoverPic = entity.ShowCoverPic;
                    materialEntity.NeedOpenComment = entity.NeedOpenComment.ToInt();
                    materialEntity.OnlyFansCanComment = entity.OnlyFansCanComment.ToInt();
                    //上传图文
                    mediaId = mPMaterialBll.UploadNews(materialEntity).Result;
                    break;
                case 2://图片
                case 3://语音
                    mediaId = mPMaterialBll.UploadForeverMedia(materialEntity).Result;
                    break;
                default:
                    break;
            }
            return mediaId;
        }
    }
}
